package oakclub.service;

import oakclub.background.CreateUserBackground;
import oakclub.background.ResetDailyBackground;
import oakclub.entity.BackgroundEntity;
import oakclub.entity.UserEntity;
import oakclub.global.Container;
import oakclub.model.UserModel;
import oakclub.background.LoginBackground;
import oakclub.utility.DateUtility;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;

public class UserService
{
    private static final int PROFILE_ID_RADIX = 34;
    private static final int SECRET_CODE = 99999;

    private final Logger logger = LoggerFactory.getLogger(UserService.class);

    public static String toProfileId(long userId)
    {
        return Long.toString(userId + SECRET_CODE, PROFILE_ID_RADIX);
    }

    public static long toUserId(String profileId) throws IllegalArgumentException, IllegalStateException
    {
        if(profileId == null)
        {
            throw new IllegalArgumentException("profile id null");
        }

        long userId = Long.parseLong(profileId, PROFILE_ID_RADIX) - SECRET_CODE;
        if(userId <= 0)
        {
            throw new IllegalStateException("user id must be greater than 0");
        }

        return userId;
    }

    public UserEntity login(long userId, String deviceName, String osVersion, String deviceToken) throws Exception
    {
        // get user
        UserModel userModel = (UserModel) Container.getComponent(UserModel.class);
        UserEntity user = userModel.get(userId);

        // create if not exists
        if(user == null)
        {
            user = this.create(userId);
        }

        // reset daily
        Date lastLogin = user.getLastLogin();
        if(lastLogin == null || DateUtility.isBeforeToday(lastLogin))
        {
            this.resetDaily(userId);
        }

        // background
        GearmanService gearmanService = (GearmanService) Container.getComponent(GearmanService.class);
        gearmanService.submitBackground(LoginBackground.class, new BackgroundEntity("user_id", userId, "device_name", deviceName, "os_version", osVersion, "device_token", deviceToken));

        return user;
    }

    private UserEntity create(long userId) throws Exception
    {
        // get data from fb
        FacebookService facebookService = (FacebookService) Container.getComponent(FacebookService.class);
        UserEntity fbUser = facebookService.getUser(userId);

        // background
        GearmanService gearmanService = (GearmanService) Container.getComponent(GearmanService.class);
        gearmanService.submitBackground(CreateUserBackground.class, new BackgroundEntity("user_id", userId));

        return null;
    }

    private void resetDaily(long userId)
    {
        // background
        GearmanService gearmanService = (GearmanService) Container.getComponent(GearmanService.class);
        gearmanService.submitBackground(ResetDailyBackground.class, new BackgroundEntity("user_id", userId));
    }

    public void setLocale(long userId, String locale) throws Exception
    {
        UserModel userModel = (UserModel) Container.getComponent(UserModel.class);
        userModel.setLocale(userId, locale);
    }

    public void verify(long userId) throws Exception
    {
        UserModel userModel = (UserModel) Container.getComponent(UserModel.class);
        userModel.verify(userId);
    }

    public void skipVerification(long userId) throws Exception
    {
        UserModel userModel = (UserModel) Container.getComponent(UserModel.class);
        userModel.skipVerification(userId);
    }
}
